<template>
    <div>
        <el-table v-loading="loading" :data="list" element-loading-text="加载中..." style="width: 100%">
            <el-table-column label="序号" prop="sort" align="center" width="80">
                <template slot-scope="scope">
                    <span>{{ (scope.$index + 1) + ((page.page-1) * page.size) }}</span>
                </template>
            </el-table-column>

            <el-table-column label="名称" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.name }}</span>
                </template>
            </el-table-column>

            <el-table-column label="图片" align="center">
                <template slot-scope="{row}">
                    <el-image :src="row.coverUrl" style="width: 108px; height: 60px; border-radius: 4px;" fit="cover" />
                </template>
            </el-table-column>

            <el-table-column label="归属地" align="center">
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.template_own_type" :value="row.position"/>
                </template>
            </el-table-column>

            <el-table-column label="套系分类" align="center">
                <template slot-scope="{row}">
                    <dict-tag :options="dict.type.template_cat_type" :value="row.type"/>
                </template>
            </el-table-column>

            <el-table-column label="权重" align="center">
                <template slot-scope="{row}">
                    <span>{{ row.weight }}</span>
                </template>
            </el-table-column>

            <el-table-column label="状态" align="center">
                <template slot-scope="{row}">
                    <el-switch v-model="row.status" :active-value="1" :inactive-value="0"
                        @change="handleStatusChange(row)" />
                </template>
            </el-table-column>

            <el-table-column label="创建时间" align="center">
                <template slot-scope="{row}">
                    <span>{{ parseTime(row.createDate) }}</span>
                </template>
            </el-table-column>

            <el-table-column label="修改时间" align="center">
                <template slot-scope="{row}">
                    <span>{{ parseTime(row.lastUpdate) }}</span>
                </template>
            </el-table-column>

            <el-table-column label="操作" align="center" width="200">
                <template slot-scope="{row}">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="onEdit(row)">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="onDelete(row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script>
import { delBanner, updateBanner } from '@/api/banner'

export default {
    props: {
        loading: {
            type: Boolean,
            default() {
                return false
            }
        },
        list: {
            type: Array,
            default() {
                return []
            }
        },
        page: {
            type: Object,
            default() {
                return {
                    page: 1,
                    size: 10
                }
            }
        }
    },
    dicts: ['template_cat_type', 'template_own_type'],
    methods: {
        // 处理状态改变
        async handleStatusChange(row) {
            try {
                updateBanner({ uuid: row.uuid, status: row.status }).then (res => {
                this.$message.success('状态更新成功')
                })
            } catch (error) {
                console.error('更新状态失败:', error)
                this.$message.error('状态更新失败')
                row.status = row.status === 1 ? 0 : 1 // 恢复原状态
            }
        },
        // 修改
        onEdit(row) {
            this.$emit('edit', row.uuid)
        },
        // 删除
        onDelete(row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                return delBanner(row.uuid)
            }).then(() => {
                this.$emit('delete')
                this.$modal.msgSuccess("删除成功")
            })
        }
    }

}
</script>

<style lang="scss" scoped></style>